package org.plex.core.imp;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.plex.core.MultiTypeVariable;
import org.plex.core.TaskSearchManager;
import org.plex.model.Assignment;
import org.plex.model.ProcessInstance;
import org.plex.model.Task;
import org.plex.model.User;
import org.plex.model.imp.AssignmentImp;
import org.plex.model.imp.ProcessImp;
import org.plex.model.imp.ProcessInstanceImp;
import org.plex.model.imp.TaskImp;
import org.plex.model.imp.UserImp;
import org.plex.searchfilter.TaskFilter;
import org.plex.searchfilter.imp.TaskFilterImp;

import com.adobe.idp.dsc.clientsdk.ServiceClientFactory;
import com.adobe.idp.taskmanager.dsc.client.TaskManagerClientFactory;
import com.adobe.idp.taskmanager.dsc.client.TaskManagerQueryService;
import com.adobe.idp.taskmanager.dsc.client.query.ProcessInstanceRow;
import com.adobe.idp.taskmanager.dsc.client.query.ProcessRow;
import com.adobe.idp.taskmanager.dsc.client.query.TMUser;
import com.adobe.idp.taskmanager.dsc.client.query.TaskQueryServiceException;
import com.adobe.idp.taskmanager.dsc.client.query.TaskRow;
import com.adobe.idp.um.api.infomodel.impl.UserImpl;

public class TaskSearchManangerImp implements TaskSearchManager
{

	private ServiceClientFactory serviceClientFactory;

	private TaskManagerQueryService taskManagerQueryService;

	public TaskSearchManangerImp(ServiceClientFactory serviceClientFactory)
			throws NullPointerException
	{
		if (null == serviceClientFactory)
		{
			throw new NullPointerException();
		}
		this.setServiceClientFactory(serviceClientFactory);
		taskManagerQueryService = TaskManagerClientFactory
				.getQueryManager(serviceClientFactory);
	}

	@SuppressWarnings("unchecked")
	public List<MultiTypeVariable> getProcessVariable(String processName)
			throws TaskQueryServiceException
	{
		List list = taskManagerQueryService.getProcessVariables(processName);
		List<MultiTypeVariable> muList = new ArrayList<MultiTypeVariable>();
		Iterator iterator = list.iterator();
		while (iterator.hasNext())
		{
			com.adobe.idp.taskmanager.dsc.client.query.MultiTypeVariable multiTypeVariable = (com.adobe.idp.taskmanager.dsc.client.query.MultiTypeVariable) iterator
					.next();
			muList.add(new MultiTypeVariableImp(multiTypeVariable));
		}
		return muList;
	}

	@SuppressWarnings("unchecked")
	public List<MultiTypeVariable> getProcessVariableValues(long taskId)
			throws TaskQueryServiceException
	{
		List list = taskManagerQueryService.getProcessVariableValues(taskId);
		List<MultiTypeVariable> muList = new ArrayList<MultiTypeVariable>();
		Iterator iterator = list.iterator();
		while (iterator.hasNext())
		{
			com.adobe.idp.taskmanager.dsc.client.query.MultiTypeVariable multiTypeVariable = (com.adobe.idp.taskmanager.dsc.client.query.MultiTypeVariable) iterator
					.next();
			muList.add(new MultiTypeVariableImp(multiTypeVariable));
		}
		return muList;
	}

	public TaskFilter newTaskFilter()
	{

		return new TaskFilterImp(this.taskManagerQueryService.newTaskFilter());
	}

	@SuppressWarnings("unchecked")
	public List<ProcessInstance> processInstanceList(String processName,
			boolean returnVariables) throws TaskQueryServiceException
	{
		List list = this.taskManagerQueryService.processInstanceList(
				processName, returnVariables);
		List<ProcessInstance> processList = new ArrayList<ProcessInstance>();
		Iterator iterator = list.iterator();
		while (iterator.hasNext())
		{
			ProcessInstanceRow instanceRow = (ProcessInstanceRow) iterator
					.next();
			processList.add(new ProcessInstanceImp(instanceRow));
		}

		return processList;
	}

	@SuppressWarnings("unchecked")
	public List<org.plex.model.Process> processList()
			throws TaskQueryServiceException
	{
		List list = taskManagerQueryService.processList();
		List<org.plex.model.Process> processlist = new ArrayList<org.plex.model.Process>();
		Iterator<ProcessRow> iterator = list.iterator();
		while (iterator.hasNext())
		{
			processlist.add(new ProcessImp(iterator.next()));
		}
		return processlist;
	}

	@SuppressWarnings("unchecked")
	public List<org.plex.model.ProcessInstance> processSearch(
			com.adobe.idp.taskmanager.dsc.client.query.ProcessSearchFilter processSearchFilter)
			throws TaskQueryServiceException
	{
		List list = taskManagerQueryService.processSearch(processSearchFilter);
		List<org.plex.model.ProcessInstance> processlist = new ArrayList<org.plex.model.ProcessInstance>();
		Iterator iterator = list.iterator();
		while (iterator.hasNext())
		{
			ProcessInstanceRow row = (ProcessInstanceRow) iterator.next();
			processlist.add(new ProcessInstanceImp(row));
		}
		return processlist;
	}

	@SuppressWarnings("unchecked")
	public List<Assignment> taskHistory(long taskId)
			throws TaskQueryServiceException
	{
		List tasklist = taskManagerQueryService.taskHistory(taskId);
		List<Assignment> assList = new ArrayList<Assignment>();
		Iterator iterator = tasklist.iterator();
		while (iterator.hasNext())
		{
			com.adobe.idp.taskmanager.dsc.client.query.Assignment assignment = (com.adobe.idp.taskmanager.dsc.client.query.Assignment) iterator
					.next();
			assList.add(new AssignmentImp(assignment));
		}
		return assList;
	}

	@SuppressWarnings("unchecked")
	public List<Task> taskList(TaskFilter filter)
			throws TaskQueryServiceException
	{
		TaskFilterImp imp = (TaskFilterImp) filter;
		List list = taskManagerQueryService.taskList(imp.getTaskFilter());
		List<Task> taskList = new ArrayList<Task>();
		Iterator iterator = list.iterator();
		while (iterator.hasNext())
		{
			TaskRow taskRow = (TaskRow) iterator.next();
			taskList.add(new TaskImp(taskRow));
		}

		return taskList;
	}

	@SuppressWarnings("unchecked")
	public List<Task> taskSearch(
			com.adobe.idp.taskmanager.dsc.client.query.TaskSearchFilter taskSearchFilter)
			throws TaskQueryServiceException
	{
		List list = taskManagerQueryService.taskSearch(taskSearchFilter);
		List<Task> taskList = new ArrayList<Task>();
		Iterator iterator = list.iterator();
		while (iterator.hasNext())
		{
			TaskRow taskRow = (TaskRow) iterator.next();
			taskList.add(new TaskImp(taskRow));
		}
		return taskList;
	}

	public List<User> userSearch(
			com.adobe.idp.taskmanager.dsc.client.query.UserSearchFilter filter)
			throws TaskQueryServiceException
	{

		List<TMUser> tmList = taskManagerQueryService.userSearch(filter);
		List<User> list = new ArrayList<User>();
		for (TMUser tmUser : tmList)
		{
			UserImpl imp = new UserImpl();
			imp.setUserid(tmUser.getLoginId());
			imp.setOid(tmUser.getId());
			imp.setEmail(tmUser.getEmail());
			imp.setTelephoneNumber(tmUser.getTelephone());
			imp.setCommonName(tmUser.getDisplayName());
			list.add(new UserImp(imp));
		}
		return list;
	}

	public void setServiceClientFactory(ServiceClientFactory serviceClientFactory)
	{
		this.serviceClientFactory = serviceClientFactory;
	}

	public ServiceClientFactory getServiceClientFactory()
	{
		return serviceClientFactory;
	}

}
